<?php

namespace App\Http\Controllers\Api\V1;

use App\Enums\ResponseCode;
use App\Exceptions\BusinessException;
use App\Http\Controllers\Api\AbstractApiController;
use App\Http\Requests\System\UploadImagesRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class SystemController extends AbstractApiController
{
    const HEAD_PIC = 1;//用户头像
//    const IDENTITY_PIC = 2;//身份认证照片
//    const PAY_CODE_PIC = 3;//收款二维码
//    const PAYMENT_PIC = 4;//付款凭证
    const FEED_BACK_PIC = 5;//问题反馈
    const OTHER_FILE = 6;//其他问题

    public static array $dictionaries = [
        self::HEAD_PIC => 'head_pic',
//        self::IDENTITY_PIC => 'identity_pic',
//        self::PAY_CODE_PIC => 'pay_code',
//        self::PAYMENT_PIC => 'payment_pic',
        self::FEED_BACK_PIC => 'feed_back_pic',
        self::OTHER_FILE => 'other_files',
    ];

    /**
     * 上传图片
     * @catalog 工具
     * @title 上传图片
     * @description 上传图片
     * @method post
     * @url upload
     * @param UploadImagesRequest $uploadImagesRequest
     * @return JsonResponse
     * @throws BusinessException
     */
    public function uploadImages(UploadImagesRequest $uploadImagesRequest)
    {
        $file = $uploadImagesRequest->file('image');
        if (!empty($file)) {
            $res = $file->isValid();
            if ($res) {
                $storage = Storage::disk();
                $uploadFile = $storage->put(self::$dictionaries[$uploadImagesRequest->scene], $file);
                if (!$uploadFile) {
                    self::exception(ResponseCode::SERVICE_UNAVAILABLE, trans('上传文件失败'));
                }
//                $newFileName['save_url'] = $uploadFile;
                $newFileName['image_url'] = $storage->url($uploadFile);
            } else {
                self::exception(ResponseCode::REQUEST_HEADER_FIELDS_TOO_LARGE, trans('上传失败图片不能超多2M'));
            }
        } else {
            self::exception(ResponseCode::SERVICE_UNAVAILABLE, trans('请选择文件'));
        }
        return self::success(trans('上传图片成功'), ResponseCode::OK, $newFileName);
    }
}
